package com.goods.shop.exam;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Org{
    private String orgNo;
    private String level;//0 1 2
    private String parentNo;//上级机构号
    private String orgName;//机构名称
    private String status;//0-正常，1-已撤并
    public Org(){

    }
    public Org(String orgNo,String level,String parentNo,String orgName,String status){
        this.orgNo=orgNo;
        this.level=level;
        this.parentNo=parentNo;
        this.orgName=orgName;
        this.status=status;
    }
    public String getOrgNo() {
        return orgNo;
    }

    public void setOrgNo(String orgNo) {
        this.orgNo = orgNo;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getParentNo() {
        return parentNo;
    }

    public void setParentNo(String parentNo) {
        this.parentNo = parentNo;
    }

    public String getOrgName() {
        return orgName;
    }

    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return  orgNo +" " + level +" "+ parentNo + " " + orgName +" "+ status;
    }
}
class OrgRule{
    private String oldOrg;
    private String newOrg;

    public String getOldOrg() {
        return oldOrg;
    }

    public void setOldOrg(String oldOrg) {
        this.oldOrg = oldOrg;
    }

    public String getNewOrg() {
        return newOrg;
    }

    public void setNewOrg(String newOrg) {
        this.newOrg = newOrg;
    }
}
public class E004 {
     public List<Org> recombination(List<Org> oldOrgs,List<OrgRule> rules){
         List<Org> resultOrgs=new ArrayList<>();
//         for(OrgRule rule:rules)
         {
             OrgRule rule=rules.get(0);
             Org oldOrg=new Org();
             Org newOrg=new Org();
             String oldOrgNo=rule.getOldOrg();
             String newOrgNo=rule.getNewOrg();
             for(Org org:oldOrgs){
                 if(org.getOrgNo().equals(oldOrgNo))oldOrg=org;
                 if(org.getOrgNo().equals(newOrgNo))newOrg=org;
             }

             for(Org org:oldOrgs){
                 if(org.getOrgNo().equals(oldOrgNo)){
                     org.setStatus("1");
                 }
                 if(oldOrg.getLevel().equals("1")){//如果旧的组织机构是分行,将下在所有分行修改一下
                     if(org.getParentNo().equals(oldOrgNo)){
                             org.setParentNo(newOrgNo);
                             org.setOrgName(org.getOrgName().replaceAll(oldOrg.getOrgName(),newOrg.getOrgName()));
                     }
                 }
//                 else{
//                     //如果是支行，则将对应的父类修改为新的父类值
//                     if(org.getOrgNo().equals(oldOrgNo)){
//                         org.setParentNo(newOrg.getParentNo());
//                     }
//                 }
                 resultOrgs.add(org);
             }
         }

         return resultOrgs;
     }

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        List<String> lines=new ArrayList<>();
        List<Org> orgs=new ArrayList<>();
        E004 service=new E004();
        int index=0;
        int num=0;
        while(scanner.hasNext()){
            String line=scanner.nextLine();
            if(line.equals("-1"))break;
            if(index!=0){
                lines.add(line);

            }else{
                num=Integer.valueOf(line);
            }
            index++;
        }
        for(int i=0;i<num;i++){
            String[] datas=lines.get(i).split(" ");
            Org org=new Org(datas[0],datas[1],datas[2],datas[3],datas[4]);
            orgs.add(org);

        }
        List<OrgRule> rules=new ArrayList<>();
        for(int i=num;i<lines.size();i++){
            String[] splitOrgs=lines.get(i).split(" ");
            String oldOrg=splitOrgs[0];
            String newOrg=splitOrgs[1];
            OrgRule rule=new OrgRule();
            rule.setOldOrg(oldOrg);
            rule.setNewOrg(newOrg);
            rules.add(rule);
        }

        List<Org> resultOrgs=service.recombination(orgs,rules);
        index=0;
        for (Org resultOrg : resultOrgs) {
            if(index!=resultOrgs.size()-1){
                System.out.println(resultOrg);
            }else{
                System.out.print(resultOrg);
            }
            index++;
        }
    }
}
